<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class initPermissions extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:init-permissions';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';


    /**
     * 删除sql
     * delete from roles;
     * delete from permissions;
     * delete from role_has_permissions;
     * alter table roles AUTO_INCREMENT=1;
     * alter table permissions AUTO_INCREMENT=1;
     */

    /**
     * Execute the console command.
     */
    public function handle()
    {
        DB::statement("delete from roles;");
        DB::statement("delete from permissions;");
        DB::statement("delete from role_has_permissions;");
        DB::statement("alter table roles AUTO_INCREMENT=1;");
        DB::statement("alter table permissions AUTO_INCREMENT=1;");

        $roleSuperAddmin = Role::create(['id' => 1, 'name' => '超级管理员']); //超级管理员
        $roleCityAdmin = Role::create(['id' => 2, 'name' => '市级管理员', 'type' => 1]); //市级管理员
        $roleCityReviewer = Role::create(['id' => 3, 'name' => '市级审核员', 'type' => 1]); //市级审核员
        $roleDistrictAdmin = Role::create(['id' => 4, 'name' => '填报单位管理员', 'type' => 2]); //填报单位管理员
        $roleDistrictReporting = Role::create(['id' => 5, 'name' => '填报单位填报员', 'type' => 2]); //填报单位填报员
        $roleDistrictReviewer = Role::create(['id' => 6, 'name' => '填报单位审核员', 'type' => 2]); //填报单位审核员

        Permission::create(['id' => 1, 'name' => 'dashbord']); //看版
        Permission::create(['id' => 2, 'name' => 'report-template']); //数据模板
        Permission::create(['id' => 3, 'name' => 'task-template']); //任务模板
        Permission::create(['id' => 4, 'name' => 'task-publish']); //任务发布
        Permission::create(['id' => 5, 'name' => 'sub-task-report']); //工单填报
        Permission::create(['id' => 6, 'name' => 'sub-task-review1']); //工单审核 初审
        Permission::create(['id' => 7, 'name' => 'sub-task-review2']); //工单审核 复审
        Permission::create(['id' => 8, 'name' => 'aggregate-report']); //数据报表
        Permission::create(['id' => 9, 'name' => 'dict']); //基础数据

        $roleSuperAddmin->syncPermissions([
            'dashbord',
            'report-template',
            'task-template',
            'task-publish',
            'sub-task-report',
            'sub-task-review1',
            'sub-task-review2',
            'aggregate-report',
            'dict'
        ]);

        $roleCityAdmin->syncPermissions([
            'dashbord',
            'report-template',
            'task-template',
            'task-publish',
            'aggregate-report',
            'dict'
        ]);

        $roleCityReviewer->syncPermissions([
            'task-template',
            'sub-task-review2',
        ]);

        $roleDistrictAdmin->syncPermissions([
            'sub-task-report',
            'sub-task-review1',
            'dict',
        ]);

        $roleDistrictReporting->syncPermissions([
            'sub-task-report',
        ]);

        $roleDistrictReviewer->syncPermissions([
            'sub-task-review1',
        ]);

        echo "\ncompleted!";
        exit;
    }
}
